package com.rpframework.module.common.bottom.controller;

import com.rpframework.core.json.JsonResp;
import com.rpframework.core.json.ParameterException;
import com.rpframework.module.common.config.Config;
import com.rpframework.module.common.url.RequestDescription;
import com.rpframework.utils.ArrayUtils;
import com.rpframework.utils.PackageUtils;
import com.rpframework.utils.StringUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@RequestMapping
@RequestDescription("实体数据枚举,前段展示数据")
@ResponseBody
public class EnumController {

    private static String[] commonEnumPackages = new String[]{Config.commonBottomPackage + ".entity.enums"};

    public EnumController(String[] packages) {
        // 包扫描
        if (StringUtil.isBlank(packages)) {
            packages = commonEnumPackages;
        } else {
            packages = ArrayUtils.concatAll(packages, commonEnumPackages);
        }
        enums = PackageUtils.scanEnumClassMap(packages);
    }

    /**
     * 所有枚举类
     */
    public Map<String, Map<String, Map<String, Object>>> enums;

    @RequestMapping
    @RequestDescription("获取所有数据")
    public JsonResp<Map<String, Map<String, Map<String, Object>>>> data() {
        return JsonResp.ok(enums);
    }

    /**
     * @param type 可以为空,枚举类简单名称
     */
    @RequestMapping
    @RequestDescription("获取所有数据")
    public JsonResp<Map<String, Map<String, Object>>> type(@RequestParam String type) {
        Map<String, Map<String, Object>> stringMapMap = enums.get(type);
        if (stringMapMap == null) {
            throw new ParameterException("没有这个类型{" + type + "}");
        }
        return new JsonResp<Map<String, Map<String, Object>>>().data(stringMapMap);
    }
}
